<template>
  <div
    :class="animate"
    :style="{ height: height, width: width }"
    :id="chartOption.bindingDiv"
    ref="chartDiv"
  ></div>
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "@/views/dashboard/mixins/resize";

import "../../animate/animate.css";

import { parseQueryString, fIsUrL } from "../../util/urlUtil";
import { linkChart } from "../../util/LinkageChart";
import { chartApiBar, remoteChartApi,chartBIanalysis } from "@/api/tool/datav/chartApi";

import { addOption } from "../../codegen/codegen";

import { customThreeDBarBIanalysis } from "@/api/tool/datav/chartBI/chartCustomThreeDBarBI";
import { getFormsource } from "@/api/tool/datav/formsource";
import { dealWithData,testData } from "../../util/utilFun.js";
const animationDuration = 6000;
import VueEvent from "@/views/tool/datav/VueEvent";
import { websocketCommand } from "../../util/websocket.js";

const CubeLeft = echarts.graphic.extendShape({
  shape: {},
  buildPath: function(ctx, shape) {
    // const yAxisPoint = shape.yAxisPoint
    // const c0 = [shape.x, shape.y]
    // const c1 = [shape.x + threeDBarWidth, shape.y - threeDBarHeight]
    // const c2 = [yAxisPoint[0] + threeDBarWidth, yAxisPoint[1] - threeDBarHeight]
    // const c3 = [yAxisPoint[0], yAxisPoint[1]]
    ctx
      .moveTo(shape.c1[0], shape.c1[1])
      .lineTo(shape.c2[0], shape.c2[1])
      .lineTo(shape.c3[0], shape.c3[1])
      .lineTo(shape.c4[0], shape.c4[1])
      .closePath();
  }
});
const CubeRight = echarts.graphic.extendShape({
  shape: {},
  buildPath: function(ctx, shape) {
    // const yAxisPoint = shape.yAxisPoint
    // const c1 = [shape.x, shape.y]
    // const c2 = [yAxisPoint[0], yAxisPoint[1]]
    // const c3 = [yAxisPoint[0], yAxisPoint[1] + threeDBarHeight]
    // const c4 = [shape.x , shape.y + threeDBarHeight]
    ctx
      .moveTo(shape.c1[0], shape.c1[1])
      .lineTo(shape.c2[0], shape.c2[1])
      .lineTo(shape.c3[0], shape.c3[1])
      .lineTo(shape.c4[0], shape.c4[1])
      .closePath();
  }
});
const CubeTop = echarts.graphic.extendShape({
  shape: {},
  buildPath: function(ctx, shape) {
    // const c1 = [shape.x, shape.y]
    // const c2 = [shape.x, shape.y + threeDBarHeight]
    // const c3 = [shape.x + threeDBarWidth, shape.y]
    // const c4 = [shape.x + threeDBarWidth, shape.y - threeDBarHeight]
    ctx
      .moveTo(shape.c1[0], shape.c1[1])
      .lineTo(shape.c2[0], shape.c2[1])
      .lineTo(shape.c3[0], shape.c3[1])
      .lineTo(shape.c4[0], shape.c4[1])
      .closePath();
  }
});
echarts.graphic.registerShape("CubeLeft", CubeLeft);
echarts.graphic.registerShape("CubeRight", CubeRight);
echarts.graphic.registerShape("CubeTop", CubeTop);

export default {
  mixins: [resize],

  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "100%"
    },
    chartOption: {
      type: Object
    },
    drawingList: {
      type: Array
    }
  },
  data() {
    return {
      chart: null,
      //dataOption: this.chartOption,
      timer: "",
      wsObj: null,
      wsFlag: false,
      wsTimer: "",
      wsValue: null,
      animate: this.className
    };
  },
  watch: {
    width() {
      this.$nextTick(() => {
        this.chart.resize();
      });
    },
    height() {
      this.$nextTick(() => {
        this.chart.resize();
      });
    },
    chartOption: {
      deep: true,
      handler(newVal, oldValue) {
        this.setOptions(JSON.parse(JSON.stringify(newVal)));
      }
    },
    "chartOption.theme": {
      handler() {
        this.chart.dispose();
        this.chart = null;
        this.initChart();
      }
    },
    "chartOption.timeout": {
      handler() {
        this.chart.dispose();
        this.chart = null;
        this.initChart();
      }
    },
    "chartOption.dataSourceType": {
      handler(value) {
        //切换数据源重新连接
        if (value !== "websocket") {
          this.wsFlag = false;
          //每次切换数据源恢复原来动画
          this.animate = this.className;
        }
        if (value !== "url") {
          clearTimeout(this.timer);
        } 
      }
    },
    "chartOption.interfaceKey": {
      handler(newValue, oldValue) {
        //初始化不创建连接，改变接口地址重新创建
        if (oldValue !== undefined) {
          this.wsValue = null;
          this.closeWs();
          this.createWs();
          this.animate = this.className;
        }
      }
    },
    wsFlag: {
      handler(value) {
        if (!value) {
          this.wsValue = null;
          this.closeWs();
        }
      }
    },
    className: {
      handler(value) {
        this.animate = value;
      }
    }
  },
  mounted() {
    //this.$nextTick(() => {
    this.initChart();
    //});
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
    clearTimeout(this.timer);
    if (this.wsFlag) {
      this.closeWs();
    }
  },
  methods: {
    initChart() {
      echarts.registerTheme("customTheme", this.chartOption.theme);
      this.chart = echarts.init(
        document.getElementById(this.chartOption.bindingDiv),
        "customTheme"
      );
     
      //判断是否开启定时器，当是访问url接口时候并且timeout不为0时候开启定时器
      if (this.timer != "") {
        clearTimeout(this.timer);
      }
      if (
        (this.chartOption.dataSourceType == 'url' || this.chartOption.dataSourceType == 'database') &&
        this.chartOption.timeout > 0
      ) {
        let timerTask = () => {
          this.setOptions(JSON.parse(JSON.stringify(this.chartOption)));
          this.timer = setTimeout(() => {
            timerTask();
          }, this.chartOption.timeout);
        };

        timerTask();
      }else{
         this.setOptions(JSON.parse(JSON.stringify(this.chartOption)));
      }
    },
    setOptions: async function(dataOption) {
      // console.log("remote=>", dataOption.remote);
      //初始化数据，判断数据源
      //如果不是静态数据就调用接口给staticDataValue赋值
      if (dataOption.dataSourceType == "url") {
        let paramJson = parseQueryString(dataOption.requestParameters);
        //let interfaceURL = 'http://localhost:8088/chart/api/bar';
        let interfaceURL = dataOption.interfaceURL;
        dataOption.staticDataValue = await chartApiBar(
          dataOption.requestMethod,
          interfaceURL,
          paramJson,
          dataOption.token
        );
        //先判断输入的url是否合法
        // if(fIsUrL(dataOption.interfaceURL)) {
        //   try {
        //     //ajax请求
        //     var paramJson = parseQueryString(dataOption.requestParameters);
        //   } catch (error) {

        //   }
        // }

        //console.log(paramJson);
      } else if (dataOption.dataSourceType == "database") {
        if (
          dataOption.database.executeSql != undefined &&
          dataOption.database.executeSql != ""
        ) {
          let result = [];
          if(dataOption.database.sqlType != 'custom'){
          
            result = await customThreeDBarBIanalysis(dataOption.database);
          }else{
            result = await chartBIanalysis(dataOption.database);
          }
          if (result.code == 200) {
            dataOption.staticDataValue = result.data;
           
            // console.log("data", result,dataOption.staticDataValue)
          } else {
            alert("操作数据库错误");
          }
        }
      } else if (dataOption.dataSourceType == "form") {
        if (dataOption.formSource != undefined) {
          let response = await getFormsource(dataOption.formSource.id);
          dataOption.staticDataValue = JSON.parse(response.data.formData);
        }
      } else if (dataOption.dataSourceType == "websocket") {
        if (
          dataOption.interfaceKey != undefined &&
          dataOption.interfaceKey != ""
        ) {
          if (!this.wsFlag) {
            this.wsFlag = true;
            this.wsValue = null;
            this.createWs();
          }
          if (this.wsValue != null) {
            dataOption.staticDataValue = this.wsValue;
          }
        }
      }

      if (dataOption.animate != null) {
        //添加动画样式
        //animateUtil.addAnimate(dataOption.bindingDiv, dataOption.animate);
      }
      //数据转化
      if(dataOption.newDataFormat == true && !(dataOption.dataSourceType == 'database' && dataOption.bi != null)){
        let sortData = dealWithData(dataOption.staticDataValue,"name");
        let staticValue = {};
        let axisData = [];
        let value = [];
        sortData.forEach(element => {
          axisData.push(element.type)
          value.push(element.data[0].value)
        });
        staticValue.axisData = axisData;
        staticValue.data = [{"value":value}]
        dataOption.staticDataValue = staticValue;
      }
      this.createBar(this.chart, dataOption);
    }, //setoptions 方法结束

    createBar(myChart, dataOption) {
      //画图方法
      //坐标轴的值
      
      var axisData = dataOption.staticDataValue.axisData;
      dataOption.yAxis.data = axisData;

      let maxTest = dataOption.max == undefined ? 100:dataOption.max;
      
      let maxData = new Array(axisData.length).fill(
        maxTest
      );

      
      //显示标签的柱图的值
      dataOption.series[2].data = dataOption.staticDataValue.data[0].value;
      var labelFormatter = e => {
        return `${e.value}` + dataOption.labelText;
      };
      dataOption.series[2].label.formatter = labelFormatter;

      //构建占比阴影部分
      if (dataOption.showAll) {
        var shadowRenderItem = (params, api) => {
          const location = api.coord([api.value(0), api.value(1)]);
          const yAxisPoint = api.coord([0, api.value(1)]);
          return {
            type: "group",
            children: [
              {
                type: "CubeLeft",
                shape: {
                  api,
                  c1: [location[0], yAxisPoint[1]],
                  c2: [
                    location[0] + dataOption.threeDBarWidth,
                    yAxisPoint[1] - dataOption.threeDBarHeight
                  ],
                  c3: [
                    yAxisPoint[0] + dataOption.threeDBarWidth,
                    yAxisPoint[1] - dataOption.threeDBarHeight
                  ],
                  c4: [yAxisPoint[0], yAxisPoint[1]]
                },
                style: {
                  fill: dataOption.shadowColor
                },
                silent: true
              },
              {
                type: "CubeRight",
                shape: {
                  api,
                  c1: [location[0], yAxisPoint[1]],
                  c2: [yAxisPoint[0], yAxisPoint[1]],
                  c3: [
                    yAxisPoint[0],
                    yAxisPoint[1] + dataOption.threeDBarHeight
                  ],
                  c4: [location[0], yAxisPoint[1] + dataOption.threeDBarHeight]
                },
                style: {
                  fill: dataOption.shadowColor
                },
                silent: true
              },
              {
                type: "CubeTop",
                shape: {
                  api,
                  c1: [location[0], yAxisPoint[1]],
                  c2: [
                    location[0] + dataOption.threeDBarWidth,
                    yAxisPoint[1] - dataOption.threeDBarHeight
                  ],
                  c3: [location[0] + dataOption.threeDBarWidth, yAxisPoint[1]],
                  c4: [location[0], yAxisPoint[1] + dataOption.threeDBarHeight]
                },
                style: {
                  fill: dataOption.shadowColor
                },
                silent: true
              }
            ]
          };
        };
        dataOption.series[0].renderItem = shadowRenderItem;
        dataOption.series[0].data = maxData;
      }
      //构建实际数值显示部分
      var currentRenderItem = (params, api) => {
        const location = api.coord([api.value(0), 0]);
        const yAxisPoint = api.coord([0, api.value(1)]);
        return {
          type: "group",
          children: [
            {
              type: "CubeLeft",
              shape: {
                api,
                c1: [location[0], yAxisPoint[1]],
                c2: [
                  location[0] + dataOption.threeDBarWidth,
                  yAxisPoint[1] - dataOption.threeDBarHeight
                ],
                c3: [
                  yAxisPoint[0] + dataOption.threeDBarWidth,
                  yAxisPoint[1] - dataOption.threeDBarHeight
                ],
                c4: [yAxisPoint[0], yAxisPoint[1]]
              },
              style: {
                fill: new echarts.graphic.LinearGradient(1, 0, 0, 0, [
                  {
                    offset: 0,
                    color: dataOption.processColor[1]
                  },
                  {
                    offset: 1,
                    color: dataOption.processColor[0]
                  }
                ])
              }
            },
            {
              type: "CubeRight",
              shape: {
                api,
                c1: [location[0], yAxisPoint[1]],
                c2: [yAxisPoint[0], yAxisPoint[1]],
                c3: [yAxisPoint[0], yAxisPoint[1] + dataOption.threeDBarHeight],
                c4: [location[0], yAxisPoint[1] + dataOption.threeDBarHeight]
              },
              style: {
                fill: new echarts.graphic.LinearGradient(1, 0, 0, 0, [
                  {
                    offset: 0,
                    color: dataOption.processColor[1]
                  },
                  {
                    offset: 1,
                    color: dataOption.processColor[0]
                  }
                ])
              }
            },
            {
              type: "CubeTop",
              shape: {
                api,
                c1: [location[0], yAxisPoint[1]],
                c2: [
                  location[0] + dataOption.threeDBarWidth,
                  yAxisPoint[1] - dataOption.threeDBarHeight
                ],
                c3: [location[0] + dataOption.threeDBarWidth, yAxisPoint[1]],
                c4: [location[0], yAxisPoint[1] + dataOption.threeDBarHeight]
              },
              style: {
                fill: new echarts.graphic.LinearGradient(1, 0, 0, 0, [
                  {
                    offset: 0,
                    color: dataOption.processColor[1]
                  },
                  {
                    offset: 1,
                    color: dataOption.processColor[1]
                  }
                ])
              }
            }
          ]
        };
      };
      dataOption.series[1].data = dataOption.staticDataValue.data[0].value;
      dataOption.series[1].renderItem = currentRenderItem;

      let grid = {
        left: dataOption.gridLeft + "%",
        right: "10%",
        bottom: "12%",
        top: "15%",
        containLabel: true
      };

      let option = {
        title: dataOption.title,
        tooltip: dataOption.tooltip,
        legend: dataOption.legend,
        grid: grid,
        series: dataOption.series,
        xAxis: dataOption.xAxis,
        yAxis: dataOption.yAxis
      };

     //执行通用处理函数
      if(dataOption.customData != undefined && dataOption.customData != ""){
        try {
          option = (
          
            //里面为要处理的代码块
          eval(dataOption.customData) 
          
          )(
            option,
            dataOption.staticDataValue,
            this.$refs.chartDiv
          );
        } catch (error) {
          console.log(error)
        }
       }
        //交互组件配置
      if(dataOption.interactData != undefined && dataOption.interactData != ""){
          try {
              option = (        
              //里面为要处理的代码块
              eval(dataOption.interactData)             
            )(
            option,
            dataOption.staticDataValue,
            this.$refs.chartDiv
            );
          } catch (error) {
            console.log(error)
          }               
       }      
      this.chart.setOption(option, true);
      //将setting存入map集合
      var setting = {
        bindingType: "CustomThreeDBar",
        option: option,
        // stack: stack,
        // info: info,
        showAll: dataOption.showAll,
        shadowColor: dataOption.shadowColor,
        color: dataOption.processColor,
        param1: dataOption.threeDBarWidth,
        param2: dataOption.threeDBarHeight
      };
      addOption(dataOption.bindingDiv, setting);

      //开启图表联动
      if (dataOption.isLink == true) {
        this.chart.on("click", params => {
          this.chart.off("click");
          //设置参数
          let arrObject = {
            legendName: params.name,
            seriesName: params.seriesName,
            data: params.value
          };

          let arrs = JSON.stringify(arrObject);

          //获取绑定的图表
          let bindList = this.chartOption.bindList;

          if (bindList.length > 0) {
            linkChart(dataOption.arrName, arrs, bindList, this.drawingList);
          }
        });
      }
      // else{
      //   //关闭图表联动，取消echart点击事件
      //   this.chart.off('click');
      // }

      //开启图表下钻
      else if (dataOption.isDrillDown == true) {
        this.chart.off("click");
        this.chart.on("click", params => {
          //设置参数
          let arrObject = {
            legendName: params.name,
            seriesName: params.seriesName,
            data: params.value
          };

          let arrs = JSON.stringify(arrObject);

          //获取绑定的图表
          let drillDownChartOption = this.chartOption.drillDownChartOption;

          if (
            drillDownChartOption != undefined &&
            drillDownChartOption != null
          ) {
            this.$set(
              this.chartOption.drillDownChartOption.chartOption,
              "requestParameters",
              "drillParam=" + arrs
            );
            //发送下钻消息
            VueEvent.$emit(
              "drill_down_msg",
              this.chartOption.drillDownChartOption
            );
          }
        });
      } else {
        //关闭图表联动，取消echart点击事件
        this.chart.off("click");
      }

      //开启远程图表控制
      if (dataOption.isRemote == true) {
        if (dataOption.remote != undefined && dataOption.remote != null) {
          this.chart.off("click");
          this.chart.on("click", params => {
            //设置参数
            let arrObject = {
              legendName: params.name,
              seriesName: params.seriesName,
              data: params.value
            };

            let remoteData = { ...dataOption.remote };
            remoteData.query = arrObject;
            //调用接口
            remoteChartApi(remoteData);
          });
        }
      }
    },
    //创建图表方法结束

    //  ws连接成功，后台返回的ws数据
    receiveMsg(e) {
      if (e.action == "chat") {
        // console.log('服务端推送',e);
        this.wsValue = JSON.parse(e.result);
        this.setOptions(JSON.parse(JSON.stringify(this.chartOption)));
      }
    },
    // 建立连接、发起ws请求，以心跳方式，向服务端发送数据
    createWs() {
      this.wsObj = new WebSocket(process.env.VUE_APP_WEBSOCKET);
      // 若为对象类型，以回调方式发送
      websocketCommand(
        this.wsObj,
        "create",
        5000,
        this.chartOption.interfaceKey,
        this.sendHeartBeat,
        this.receiveMsg,
        this.reconnectWs,
        this.wsWarning
      );
    },
    // 断网重连，需要处理的逻辑
    reconnectWs() {
      if (this.wsObj) {
        console.log("%c websocket_reconnect", "color:blue");
        this.createWs();
      }
    },
    // 以回调方式向服务端发送(对象类型的)心跳
    sendHeartBeat() {
      if (this.wsTimer != "") {
        clearTimeout(this.wsTimer);
      }

      let wsTimerTask = () => {
        console.log("发送心跳", "ping");
        let obj = { action: "keep", key: this.chartOption.interfaceKey };
        this.wsObj.send(JSON.stringify(obj));
        this.wsTimer = setTimeout(() => {
          wsTimerTask();
        }, parseInt(process.env.VUE_APP_WEBSOCKET_TIMEOUT));
        this.wsWarning();
      };

      wsTimerTask();
    },
    wsWarning() {
      //如果断开连接则显示组件警告动画并清除计数器
      if (this.wsObj.readyState == 3) {
        this.animate = "warning";
        clearTimeout(this.wsTimer);
      }
    },
    closeWs() {
      clearTimeout(this.wsTimer);
      websocketCommand(
        this.wsObj,
        "close",
        5000,
        this.chartOption.interfaceKey,
        this.sendHeartBeat,
        this.receiveMsg,
        this.reconnectWs,
        this.wsWarning
      );
      this.wsObj = null;
    }
  }
};
</script>
